package cn.iocoder.yudao.module.exam.controller.admin.paper;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.exam.controller.admin.paper.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.paper.PaperDO;
import cn.iocoder.yudao.module.exam.service.paper.PaperService;

@Tag(name = "管理后台 - 试卷")
@RestController
@RequestMapping("/exam/paper")
@Validated
public class PaperController {

    @Resource
    private PaperService paperService;

    @PostMapping("/create")
    @Operation(summary = "创建试卷")
    @PreAuthorize("@ss.hasPermission('exam:paper:create')")
    public CommonResult<Long> createPaper(@Valid @RequestBody PaperSaveReqVO createReqVO) {
        return success(paperService.createPaper(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新试卷")
    @PreAuthorize("@ss.hasPermission('exam:paper:update')")
    public CommonResult<Boolean> updatePaper(@Valid @RequestBody PaperSaveReqVO updateReqVO) {
        paperService.updatePaper(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除试卷")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('exam:paper:delete')")
    public CommonResult<Boolean> deletePaper(@RequestParam("id") Long id) {
        paperService.deletePaper(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得试卷")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('exam:paper:query')")
    public CommonResult<PaperRespVO> getPaper(@RequestParam("id") Long id) {
        PaperRespVO paper = paperService.getPaper(id);
        return success(paper);
    }

    @GetMapping("/page")
    @Operation(summary = "获得试卷分页")
    @PreAuthorize("@ss.hasPermission('exam:paper:query')")
    public CommonResult<PageResult<PaperRespVO>> getPaperPage(@Valid PaperPageReqVO pageReqVO) {
        PageResult<PaperDO> pageResult = paperService.getPaperPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PaperRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出试卷 Excel")
    @PreAuthorize("@ss.hasPermission('exam:paper:export')")
    @OperateLog(type = EXPORT)
    public void exportPaperExcel(@Valid PaperPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PaperDO> list = paperService.getPaperPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "试卷.xls", "数据", PaperRespVO.class,
                        BeanUtils.toBean(list, PaperRespVO.class));
    }

}